package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_dianping;

import cn.com.api.service.ApiT_dianpingService;

import cn.com.util.*;
import cn.com.util.*;

public class T_dianpingBaseController {

	private static Logger logger = Logger.getLogger(T_dianpingBaseController.class);

	@Autowired
	private ApiT_dianpingService apiT_dianpingService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_dianping-list")
	public ModelAndView t_dianping_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_dianping_list.jsp");
		List<ApiT_dianping> t_dianpings = apiT_dianpingService.findT_dianpings();
		mv.addObject("t_dianpings", t_dianpings);
		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_dianping-add-pre")
	public ModelAndView t_dianping_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_dianping_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_dianping t_dianping = apiT_dianpingService.findT_dianpingById(Integer.valueOf(id));
			mv.addObject("t_dianping", t_dianping);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_dianping-add")
	public void t_dianping_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String c_id = request.getParameter("c_id");
		String c_sid = request.getParameter("c_sid");
		String c_jid = request.getParameter("c_jid");
		String c_dtype = request.getParameter("c_dtype");
		String c_fee = request.getParameter("c_fee");
		String c_text = request.getParameter("c_text");
		String c_userid = request.getParameter("c_userid");
		String c_ip = request.getParameter("c_ip");
		String c_appdate = request.getParameter("c_appdate");
		String c_retext = request.getParameter("c_retext");
		String c_redate = request.getParameter("c_redate");
		String c_enable = request.getParameter("c_enable");
		String c_adm = request.getParameter("c_adm");
		String c_check = request.getParameter("c_check");
		String c_guest = request.getParameter("c_guest");
		String c_goodsid = request.getParameter("c_goodsid");
		String c_orderid = request.getParameter("c_orderid");
		String is_raise = request.getParameter("is_raise");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_dianping t_dianping = new ApiT_dianping();
			/*
			 * t_dianping.setC_id(c_id); t_dianping.setC_sid(c_sid);
			 * t_dianping.setC_jid(c_jid); t_dianping.setC_dtype(c_dtype);
			 * t_dianping.setC_fee(c_fee); t_dianping.setC_text(c_text);
			 * t_dianping.setC_userid(c_userid); t_dianping.setC_ip(c_ip);
			 * t_dianping.setC_appdate(c_appdate);
			 * t_dianping.setC_retext(c_retext);
			 * t_dianping.setC_redate(c_redate);
			 * t_dianping.setC_enable(c_enable); t_dianping.setC_adm(c_adm);
			 * t_dianping.setC_check(c_check); t_dianping.setC_guest(c_guest);
			 * t_dianping.setC_goodsid(c_goodsid);
			 * t_dianping.setC_orderid(c_orderid);
			 * t_dianping.setIs_raise(is_raise);
			 */
			apiT_dianpingService.insertT_dianping(t_dianping);
		}
		// edit
		else {
			ApiT_dianping t_dianping = apiT_dianpingService.findT_dianpingById(Integer.valueOf(id));
			/*
			 * t_dianping.setC_id(c_id); t_dianping.setC_sid(c_sid);
			 * t_dianping.setC_jid(c_jid); t_dianping.setC_dtype(c_dtype);
			 * t_dianping.setC_fee(c_fee); t_dianping.setC_text(c_text);
			 * t_dianping.setC_userid(c_userid); t_dianping.setC_ip(c_ip);
			 * t_dianping.setC_appdate(c_appdate);
			 * t_dianping.setC_retext(c_retext);
			 * t_dianping.setC_redate(c_redate);
			 * t_dianping.setC_enable(c_enable); t_dianping.setC_adm(c_adm);
			 * t_dianping.setC_check(c_check); t_dianping.setC_guest(c_guest);
			 * t_dianping.setC_goodsid(c_goodsid);
			 * t_dianping.setC_orderid(c_orderid);
			 * t_dianping.setIs_raise(is_raise);
			 */
			apiT_dianpingService.updateT_dianping(t_dianping);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_dianping-delete")
	public void t_dianping_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_dianpingService.deleteT_dianping(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
